<?php

namespace app\company\model;


use think\Model;

class WarehouseStorageGoodsLogs extends Base
{
    /**
     * 新增采购记录
     * @param $item
     * @return Model|WarehouseStorageGoodsLogs
     */
    public function companyAdd($item): WarehouseStorageGoodsLogs|Model
    {
        $money  = CompanyGoodsRule::gs($item->company_id)->goodsRuleId($item->goods_rule_id)->value('money');//得到售价
        $amount = $item->quantity * $money;
        return self::create([
            'company_order_id'         => $item->company_order_id,
            'company_order_detail_id'  => $item->company_order_detail_id,
            'company_order_storage_id' => $item->id,
            'warehouse_storage_id'     => $item->warehouse_storage_id,
            'warehouse_storage_name'   => $item->warehouse_storage_name,
            'goods_id'                 => $item->goods_id,
            'goods_rule_id'            => $item->goods_rule_id,
            'goods_cate_id'            => $item->goods_cate_id,
            'supplier_id'              => $item->supplier_id,
            'company_id'               => $item->company_id,
            'quantity'                 => $item->quantity,
            'production_time'          => $item->production_time,
            'expire_time'              => $item->expire_time,
            'amount'                   => $amount,
        ]);
    }

    public static function onAfterInsert(Model $model): void
    {
        // 统计经销商日出库订单
        if($model->type == 2){
            $outboundModel = SupplierDayOutbound::where('supplier_id',$model->supplier_id)
                                                ->where('date', date('Y-m-d'))
                                                ->find();
            // 如果存在，更新goods_type_num、goods_num、amount，否则新增数据
            if ($outboundModel) {
                // 查询种类
                $goodsTypeNum = WarehouseStorageGoodsLogs::where('supplier_id', $model->supplier_id)
                    ->where('type',2)
                    ->whereTime('create_time','today')
                    ->group('goods_rule_id')
                    ->count();
                $outboundModel->goods_type_num = $goodsTypeNum;
                $outboundModel->goods_num += $model->quantity;
                $outboundModel->amount += $model->amount;
                $outboundModel->save();
            } else {
                $outboundModel = new SupplierDayOutbound();
                $outboundModel->supplier_id = $model->supplier_id;
                $outboundModel->company_id  = $model->company_id;
                $outboundModel->date = date('Y-m-d');
                $outboundModel->goods_type_num = 1;
                $outboundModel->goods_num = $model->quantity;
                $outboundModel->amount = $model->amount;
                $outboundModel->no = "CK".date('YmdHis') . mt_rand(100000, 999999);
                $outboundModel->save();
            }
            // 创建当日经销商出库明细
            SupplierDayOutboundDetail::create(['supplier_day_outbound_id' => $outboundModel->id, 'warehouse_storage_goods_logs_id' => $model->id]);
        }
    }
}